var express = require("express"),
		router = express.Router(),
		adminUser = require("../../models/adminSite/user"),
		md5 = require("md5"),
		isLogin = require("./public/is-login"),
		userFun = new adminUser();
//如果登录不能到这
router.get("/login" , isLogin.checkNotLogin , function(req , res){
	res.render("adminSite/login" , {
		title : "HXXC-博客后台管理登录"
	});
});
//登录
//如果登录不能到这
router.post("/login" , isLogin.checkNotLogin , function(req , res){
	var userInfo = req.body;
	if(!userInfo.account){
		return res.json({
			state : "error",
			message : "用户名不能为空"
		})
	};
	if(!userInfo.password){
		return res.json({
			state : "error",
			message : "密码不能为空"
		});
	};
	//判断
	userFun.getUser({account : userInfo.account} , function(error , data){
		if(error){
			return res.json({
				state : "error",
				message : "错误"
			});
		};
		if(data){
			//判断密码是否真确
			if(md5(userInfo.password) == data.password){
				req.session.admin = data;
				return res.json({
					state : "ok",
					message : "登录成功"
				});
			}else{
				return res.json({
					state : "error",
					message : "账号或密码错误"
				});
			};
		}else{
			return res.json({
				state : "error",
				message : "账号或密码错误"
			});
		};
	});
});
//退出登录
router.get("/logout" , function(req , res){
	req.session.admin = null;
	res.redirect("/admin/login");
});
//接口
module.exports = router;
